import { pgTable, serial, varchar, timestamp, text, jsonb, integer, boolean } from "drizzle-orm/pg-core"

// 用户表
export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  email: varchar("email", { length: 255 }).unique().notNull(),
  password: varchar("password", { length: 255 }),
  name: varchar("name", { length: 255 }),
  avatar: varchar("avatar", { length: 500 }),
  googleId: varchar("google_id", { length: 255 }),
  githubId: varchar("github_id", { length: 255 }),
  isEmailVerified: boolean("is_email_verified").default(false),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow().notNull(),
})

// 思维导图表
export const mindMaps = pgTable("mind_maps", {
  id: serial("id").primaryKey(),
  title: varchar("title", { length: 255 }).notNull(),
  ownerId: integer("owner_id")
    .references(() => users.id)
    .notNull(),
  content: jsonb("content").notNull(),
  isPublic: boolean("is_public").default(false),
  shareToken: varchar("share_token", { length: 255 }),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow().notNull(),
})

// 协作表
export const collaborations = pgTable("collaborations", {
  id: serial("id").primaryKey(),
  mindMapId: integer("mind_map_id")
    .references(() => mindMaps.id)
    .notNull(),
  userId: integer("user_id")
    .references(() => users.id)
    .notNull(),
  role: varchar("role", { length: 50 }).notNull(), // 'editor', 'viewer'
  createdAt: timestamp("created_at").defaultNow().notNull(),
})

// 用户订阅表
export const userSubscriptions = pgTable("user_subscriptions", {
  id: serial("id").primaryKey(),
  userId: integer("user_id")
    .references(() => users.id)
    .notNull(),
  provider: varchar("provider", { length: 50 }).notNull(), // 'lemonsqueezy'
  subscriptionId: varchar("subscription_id", { length: 255 }).notNull(),
  planId: varchar("plan_id", { length: 255 }).notNull(),
  status: varchar("status", { length: 50 }).notNull(), // 'active', 'expired', 'cancelled'
  currentPeriodStart: timestamp("current_period_start"),
  currentPeriodEnd: timestamp("current_period_end"),
  trialEndDate: timestamp("trial_end_date"),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").defaultNow().notNull(),
})

// 点数交易记录表
export const pointTransactions = pgTable("point_transactions", {
  id: serial("id").primaryKey(),
  userId: integer("user_id")
    .references(() => users.id)
    .notNull(),
  points: integer("points").notNull(),
  type: varchar("type", { length: 50 }).notNull(), // 'earn', 'spend', 'refund'
  description: text("description"),
  relatedId: varchar("related_id", { length: 255 }), // 关联的操作ID
  createdAt: timestamp("created_at").defaultNow().notNull(),
})

// 评论表
export const comments = pgTable("comments", {
  id: serial("id").primaryKey(),
  mindMapId: integer("mind_map_id")
    .references(() => mindMaps.id)
    .notNull(),
  userId: integer("user_id")
    .references(() => users.id)
    .notNull(),
  nodeId: varchar("node_id", { length: 255 }).notNull(),
  content: text("content").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
})

// 历史版本表
export const historyVersions = pgTable("history_versions", {
  id: serial("id").primaryKey(),
  mindMapId: integer("mind_map_id")
    .references(() => mindMaps.id)
    .notNull(),
  snapshot: jsonb("snapshot").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
})

// AI使用记录表
export const aiUsageRecords = pgTable("ai_usage_records", {
  id: serial("id").primaryKey(),
  userId: integer("user_id")
    .references(() => users.id)
    .notNull(),
  type: varchar("type", { length: 50 }).notNull(), // 'expand', 'merge', 'reorganize', 'qa', 'summarize'
  pointsCost: integer("points_cost").notNull(),
  mindMapId: integer("mind_map_id").references(() => mindMaps.id),
  createdAt: timestamp("created_at").defaultNow().notNull(),
})
